package com.neca.auction.pages;

import java.util.List;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.OnEvent;
import org.apache.tapestry.annotations.Persist;

import com.neca.auction.dbb.DBBroker;
import com.neca.auction.model.CurrentUser;
import com.neca.auction.model.Product;
import com.neca.auction.model.User;


public class MyProducts {
	
	@Persist
	private Product product;
	@ApplicationState
	private CurrentUser currentUser;
	@Persist
	private double highestOffer;
	@InjectPage
	private EditProduct editProduct;
	@InjectPage
	private ProductOffers productOffers;
	private boolean view=false;
	private Class nextPage;

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
	
	public String getDesShort(){
		try{
		String shortDes=product.getDescription().substring(0, 50);
		return shortDes+"....";
		}catch(Exception e){
			return product.getDescription();
		}
	}
	
	public List<Product> getAllProducts(){
		DBBroker dbbroker=DBBroker.getDbbroker();
		User u = new User("","",currentUser.getUserName(),"","","",0);
		User user=dbbroker.getUserByUserName(u);
		return dbbroker.getMyProducts(user.getUserID());
	}
	
	public double getHighestOffer() {
		System.out.println(product.getName());
		DBBroker dbbroker=DBBroker.getDbbroker();
		double price= dbbroker.getHighestOffer(product);
		if(product.getPrice()<price){
			return price;
		}
		else{
			return product.getPrice();
		}
		
	}
	
	public String getUser(){
		return currentUser.getUserName();
	}
	
	void onSubmitFromMyProductsForm(){
		System.out.println("The form was submitted!!!");
		
	}
	
	void onValidate(){
		
	}
	Object onSuccess(int productID){
		if(view==false){
		DBBroker dbbroker=DBBroker.getDbbroker();
		Product p =dbbroker.getProduct(productID);
		editProduct.setProduct(p);
		return editProduct;
		}else{
			System.out.println("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
			DBBroker dbbroker=DBBroker.getDbbroker();
			Product p =dbbroker.getProduct(productID);
			productOffers.setProduct(p);
			nextPage=ProductOffers.class;
			return nextPage;
		}
		
	}
	
	@OnEvent(component="viewButton")
	void onViewButton(){
		
		
		view=true;
		
	}


}
